#ifndef __CAL_H__
#define __CAL_H__

#define OVER (-1)

#define TRIP_HOUR		8

#define MDATA_LEN		45
#define SDATA_LEN		77
#define DTC_LEN 		4
#define MAX_DTC_NUM	20

#define SEC_BUF_SIZE	16
#define SEC_DATA_LEN	7   // [0]speed [1]rpm [2]fuel [3]cal_load [4]cool_tmp [5]pad_pos [6]fr100

#define OBD_DATA_SPD		0
#define OBD_DATA_RPM		1
#define OBD_DATA_FR		2
#define OBD_DATA_EL		3
#define OBD_DATA_CT		4
#define OBD_DATA_PAD		5
#define OBD_DATA_FR100	6

#define ACCSPEED		5
#define ACCPADPOS		30
#define BRKSPEED		30
#define OVERSPEED		120
#define IDELSPEED		0
#define SLIDINGSPEED	25
#define SLIDINGPOS		5


struct obd_data{
	int data[SEC_DATA_LEN];
	pthread_mutex_t lock;
};

struct trip_status{
	int tripstatus;
	int min_index;
	int dtc_num;
	unsigned char trip_data[MDATA_LEN * 60 * TRIP_HOUR];
	unsigned char trip_summary[SDATA_LEN];
	unsigned char dtc_data[DTC_LEN * MAX_DTC_NUM + 1];
	pthread_mutex_t lock;
};

struct car_data{
	int writepos;
	int readpos;
	int sec_data[SEC_BUF_SIZE * SEC_DATA_LEN];
	pthread_mutex_t lock;
	pthread_cond_t notempty;
	pthread_cond_t notfull;
};

/*分钟数据切片计算中间结果的数据结构*/
struct cal_result{
	int speed_top;
	int speed_avg;		// need calculation
	int rpm_avg;			// need calculation
	int rpm_max;
	int fuel_total;		// need calculation
	int fuel_avg;		// need calculation	
	int cal_load;		// need calculation
	int cool_temp;		// need calculation
	int pad_pos;			// need calculation
	int pad_pos_max;
	int pad_pos_min;
	int dist;
	int best_fuel_rate;
	int best_speed;
	int acc_num;
	int brk_num;
	int over_spd;
	int idel_spd;
	int sliding;
};

/*分钟切片数据结构*/
/*各参数类型为最终输出的数据类型(byte)*/
struct min_data{
	unsigned char data_type;			// 0
	unsigned char data_len;			// 1
	unsigned char date_yy;			// 2
	unsigned char date_mm;			// 3
	unsigned char date_dd;			// 4
	unsigned char time_hh;			// 5
	unsigned char time_mm;			// 6
	unsigned char time_ss;			// 7
	unsigned char lat_dd;				// 8
	unsigned char lat_mm1;			// 9
	unsigned char lat_mm2;			// 10
	unsigned char lat_mm3;			// 11
	unsigned char ns;					// 12
	unsigned char long_0d;			// 13
	unsigned char long_dd;			// 14
	unsigned char long_mm1;			// 15
	unsigned char long_mm2;			// 16
	unsigned char long_mm3;			// 17
	unsigned char ew;					// 18
	unsigned char ele_h;				// 19
	unsigned char ele_l;				// 20
	unsigned char top_speed;			// 21
	unsigned char best_fuel_rate;	// 22
	unsigned char best_speed;		// 23	
	unsigned char dist_h;				// 24
	unsigned char dist_l;				// 25
	unsigned char avg_speed;			// 26
	unsigned char avg_rpm_h;			// 27
	unsigned char avg_rpm_l;			// 28
	unsigned char max_rpm_h;			// 29
	unsigned char max_rpm_l;			// 30
	unsigned char total_fuel;		// 31
	unsigned char avg_fuel_h;		// 32
	unsigned char avg_fuel_l;		// 33
	unsigned char cal_load;			// 34
	unsigned char cool_temp;			// 35
	unsigned char pad_pos;			// 36
	unsigned char pad_pos_max;		// 37
	unsigned char pad_pos_min;		// 38
	unsigned char fuel_lv;			// 39
	unsigned char acc_num;			// 40
	unsigned char brk_num;			// 41
	unsigned char over_spd;			// 42
	unsigned char idel_spd;			// 43
	unsigned char sliding;			// 44
	pthread_mutex_t lock;
	pthread_cond_t dataok;
};

/*行程数据数据结构*/
/*各参数类型为最终输出的数据类型(byte)*/
struct summary_data {
	int file_index;
	unsigned char data_type;				// 0
	unsigned char data_len;				// 1
	unsigned char start_date_yy;		// 2
	unsigned char start_date_mm;		// 3
	unsigned char start_date_dd;		// 4
	unsigned char start_time_hh;		// 5
	unsigned char start_time_mm;		// 6
	unsigned char start_time_ss;		// 7
	unsigned char start_lat_dd;			// 8
	unsigned char start_lat_mm1;		//	9
	unsigned char start_lat_mm2;		// 10
	unsigned char start_lat_mm3;		//	11
	unsigned char start_ns;				// 12
	unsigned char start_long_0d;		// 13
	unsigned char start_long_dd;		// 14
	unsigned char start_long_mm1;		// 15
	unsigned char start_long_mm2;		// 16
	unsigned char start_long_mm3;		// 17
	unsigned char start_ew;				// 18
	unsigned char start_ele_h;			// 19
	unsigned char start_ele_l;			// 20
	unsigned char end_date_yy;			// 21
	unsigned char end_date_mm;			// 22
	unsigned char end_date_dd;			// 23	
	unsigned char end_time_hh;			// 24
	unsigned char end_time_mm;			//	25
	unsigned char end_time_ss;			// 26
	unsigned char end_lat_dd;			// 27
	unsigned char end_lat_mm1;			//	28
	unsigned char end_lat_mm2;			//	29
	unsigned char end_lat_mm3;			// 30
	unsigned char end_ns;					// 31
	unsigned char end_long_0d;			// 32
	unsigned char end_long_dd;			// 33
	unsigned char end_long_mm1;			// 34
	unsigned char end_long_mm2;			// 35
	unsigned char end_long_mm3;			// 36
	unsigned char end_ew;					// 37
	unsigned char end_ele_h;				// 38
	unsigned char end_ele_l;				// 39
	unsigned char trip_time;				// 40
	unsigned char air_pressure;			// 41
	unsigned char fuel_lv;				// 42
	unsigned char bat;						// 43
	unsigned char err_dist_h;			// 44
	unsigned char err_dist_l;			// 45
	unsigned char clr_dist_h;			// 46		
	unsigned char clr_dist_l;			// 47
	unsigned char temp;					// 48
	unsigned char dist_h;					// 49
	unsigned char dist_l;					// 50
	unsigned char trip_top_speed;		// 51
	unsigned char best_fuel_rate;		// 52
	unsigned char trip_best_speed;		// 53
	unsigned char trip_total_fuel_h;	// 54
	unsigned char trip_total_fuel_l;	// 55
	unsigned char trip_avg_fuel_h;		// 56
	unsigned char trip_avg_fuel_l;		// 57
	unsigned char last_fuel_lv;			// 58 not use
	unsigned char avg_cool_temp;		// 59
	unsigned char max_cool_temp;		// 60
	unsigned char pad_pos;				// 61
	unsigned char pad_pos_max;			// 62
	unsigned char pad_pos_min;			// 63
	unsigned char trip_avg_rpm_h;		// 64
	unsigned char trip_avg_rpm_l;		// 65
	unsigned char trip_max_rpm_h;		// 66
	unsigned char trip_max_rpm_l;		// 67
	unsigned char trip_acc_cnt;			// 68	
	unsigned char trip_brk_cnt;			// 69
	unsigned char trip_over_spd;		// 70
	unsigned char trip_idel_spd;		// 71
	unsigned char trip_sliding;			// 72
	unsigned char fast_time;				// 73
	unsigned char slow_time;				// 74
	unsigned char jam_time;				// 75
	unsigned char dtc_num;				// 76
	pthread_mutex_t lock;
	pthread_cond_t dataok;
};

extern struct trip_status tstatus;
extern struct car_data cdata;
extern struct min_data mdata;
extern struct summary_data sdata;
extern struct obd_data odata;

extern void init_Trip(struct trip_status *t);
extern void init_OBD_DATA(struct obd_data *o);
extern void init_CAL(struct car_data *c, struct min_data *m, struct summary_data *s);
extern void updateFileIndex(struct summary_data *s, int index);
extern void saveFileIndexToConfigFile(int num);
extern void saveCarData(struct car_data *c, int *data);
extern void updateMinFuellv(struct min_data *m, unsigned char);
extern void updateMinGPS(struct min_data *m, unsigned char *data);
extern void updateMinTime(struct min_data *m, unsigned char *data);
extern void startTripDataRecord(struct trip_status *t);
extern void clearMinIndex(struct trip_status *t);
extern void updateSummaryTimeStart(struct summary_data *s, unsigned char *data);
extern void updateSummaryTimeEnd(struct summary_data *s, unsigned char *data);
extern void updateSummaryGPSStart(struct summary_data *s, unsigned char * data);
extern void updateSummaryGPSEnd(struct summary_data *s, unsigned char * data);
extern void updateSummaryBat(struct summary_data *s, unsigned char bat);
extern void updateSummaryFuelLV(struct summary_data *s, unsigned char value);
extern void updateSummaryLastFuelLV(struct summary_data *s, unsigned char value);
extern void * CalCarData(void *data);
#endif
